package com.phiyon.mytry.files;

import com.phiyon.mytry.service.FileService;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button button = (Button)this.findViewById(R.id.button1);
		button.setOnClickListener(new ButtonClickListener());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
	private final class ButtonClickListener implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			EditText filenameText = (EditText)findViewById(R.id.editText1);
			EditText contentText  = (EditText)findViewById(R.id.editText2);
			String filename = filenameText.getText().toString();
			String content  = contentText.getText().toString();
			FileService service = new FileService(getApplicationContext());	
			try {
				if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
					service.saveToSDCard(filename, content);
					Toast.makeText(getApplicationContext(), R.string.txt_success, Toast.LENGTH_LONG);
				} else {
					Toast.makeText(getApplicationContext(), R.string.txt_fail, Toast.LENGTH_LONG);
				}
				//service.save(filename, content);
				//Toast.makeText(getApplicationContext(), R.string.txt_success, Toast.LENGTH_LONG);
			} catch (Exception e) {
				Toast.makeText(getApplicationContext(), R.string.txt_fail, Toast.LENGTH_LONG);
				e.printStackTrace();
			}
			Toast.makeText(getApplicationContext(), R.string.txt_success, Toast.LENGTH_LONG).show();
		}
		
	}
	

}
